package com.xique.door.project.miniprogram.controller;

import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.door.project.miniprogram.service.IMiniNoticeService;
import com.xique.door.service.IProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/mine/notice")
public class MiniNoticeController {
    @Autowired
    private IMiniNoticeService miniNoticeService;

    @Autowired
    private IProblemService problemService;

//  /** 公告列表 */
//  @GetMapping("/noticeList")
//  public AjaxResult getNoticeList(Integer pageNum, Integer pageSize,HttpServletRequest request) {
//    Long parkId = SecurityUtils.getParkId();
//    pageNum = pageNum != null ? pageNum : 1;
//    pageSize = pageSize != null ? pageSize : 10;
//    if (parkId != null) {
//      return AjaxResult.success(miniNoticeService.getNoticeList( Long.valueOf(parkId), pageNum, pageSize));
//    } else {
//      List<String> str = new ArrayList<>();
//      str.add("请登录并绑定小区");
//      return AjaxResult.success(null, str);
//    }
//  }

    /**
     * 公告列表
     */
    @GetMapping("/noticeList")
    public AjaxResult getNoticeList(Long parkId) {
        Long baseUserId = SecurityUtils.getBaseUserId();
        if (baseUserId != null) {
            return miniNoticeService.getNoticeList(parkId);
        } else {
            return AjaxResult.success("请登录");
        }
    }

    /**
     * 公告详情
     */
    @GetMapping("/noticeDetails")
    public AjaxResult noticeDetails(Long id, Long parkId, HttpServletRequest request) {

        Long baseUserId = SecurityUtils.getBaseUserId();
        if (baseUserId != null) {
            return miniNoticeService.getMiniNotice(id, parkId);
        } else {
            return AjaxResult.success("请登录");
        }
    }

    /**
     * 投票
     */
    @PutMapping("/noticeVote")
    public AjaxResult noticeVote(@RequestParam("ids") String ids, Long parkId, HttpServletRequest request) {
        Long baseUserId = SecurityUtils.getBaseUserId();
        if (parkId != null && baseUserId != null) {
            String[] s = ids.split(",");
            List<Long> id = new ArrayList<>();
            if (s.length > 0) {
                for (String s1 : s) {
                    id.add(Long.valueOf(s1));
                }
            }
            return problemService.insertProblem(id, parkId,null);
        } else {
            return AjaxResult.success("请登录");
        }
    }

    /**
     * 跑马灯列表
     */
    @GetMapping("/noticeHorseRaceLampList")
    public AjaxResult noticeHorseRaceLampList(HttpServletRequest request, BigDecimal lng, BigDecimal lat) {
        Long baseUserId = SecurityUtils.getBaseUserId();
        if (baseUserId != null) {
            return miniNoticeService.getHorseRaceLampList(Long.valueOf(baseUserId), lng, lat);
        } else {
            return AjaxResult.success("请登录");
        }
    }
}
